package com.durian.base.frame;

import android.app.Dialog;

import androidx.annotation.DimenRes;
import androidx.annotation.StringRes;
import androidx.fragment.app.FragmentManager;

import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.durian.base.R;
import com.durian.base.utils.StringUtils;
import com.durian.base.utils.UIUtils;
import com.durian.ui.dialog.BaseNiceDialog;
import com.durian.ui.dialog.NiceDialog;
import com.durian.ui.dialog.ViewConvertListener;
import com.durian.ui.dialog.ViewHolder;
import com.durian.ui.factory.ViewTools;
import com.durian.ui.textview.RoundButton;

/**
 * 说明：DialogIos
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2019-06-18 13:53
 * <p/>
 * 版本：version 1.0
 */
public class DialogIos {

    public interface OnClickListener {
        void onClick(BaseNiceDialog dialog, int which);
    }

    private FragmentManager mManager;
    private boolean         isConfirmDialog       = false;
    private String          rightText             = UIUtils.getString(R.string.base_confirm);
    private String          leftText              = UIUtils.getString(R.string.base_cancel);
    private OnClickListener rightListener;
    private OnClickListener leftListener;
    private String          message;
    private String          title;
    private int             gravity               = Gravity.CENTER;
    private boolean         showBottomCloseButton = false;//是否要显示底部关闭按钮
    private boolean         isCancel              = true;
    private @DimenRes int titleSize = UIUtils.dip2px(18);
    private @DimenRes int msgSize = UIUtils.dip2px(16);

    private DialogIos(FragmentManager manager) {
        this.mManager = manager;
    }

    public static DialogIos create(FragmentManager manager) {
        return new DialogIos(manager);
    }

    public DialogIos setMessageGravity(int gravity) {
        this.gravity = gravity;
        return this;
    }

    public DialogIos isCancel(boolean isCancel) {
        this.isCancel = isCancel;
        return this;
    }

    public DialogIos confirmText(String text, OnClickListener listener) {
        isConfirmDialog = true;
        isCancel = false;
        rightText = text;
        rightListener = listener;
        return this;
    }

    public DialogIos confirmText(@StringRes int text, OnClickListener listener) {
        return right(UIUtils.getString(text), listener);
    }


    public DialogIos right(String text, OnClickListener listener) {
        if (StringUtils.isNotEmpty(text)) {
            rightText = text;
        }
        rightListener = listener;
        return this;
    }

    public DialogIos right(@StringRes int text, OnClickListener listener) {
        return right(UIUtils.getString(text), listener);
    }


    public DialogIos left(@StringRes int text) {
        left(text, new OnClickListener() {
            @Override
            public void onClick(BaseNiceDialog dialog, int which) {
                dialog.dismiss();
            }
        });
        return this;
    }

    public DialogIos left(String text) {
        left(text, new OnClickListener() {
            @Override
            public void onClick(BaseNiceDialog dialog, int which) {
                dialog.dismiss();
            }
        });
        return this;
    }

    public DialogIos left(@StringRes int text, OnClickListener listener) {
        return left(UIUtils.getString(text), listener);
    }

    public DialogIos left(String text, OnClickListener listener) {
        if (StringUtils.isNotEmpty(text)) {
            leftText = text;
        }
        leftListener = listener;
        return this;
    }

    public DialogIos showBottomCloseButton(boolean show) {
        this.showBottomCloseButton = show;
        if (show) {
            isCancel = false;
        }
        return this;
    }


    public DialogIos title(String title) {
        this.title = title;
        return this;
    }

    public DialogIos title(@StringRes int title) {
        return title(UIUtils.getString(title));
    }

    public DialogIos message(String msg) {
        this.message = msg;
        return this;
    }

    public DialogIos message(@StringRes int msg) {
        return message(UIUtils.getString(msg));
    }

    /**
     * 仿ios弹窗
     */
    public void show() {
        if (mManager != null) {
            NiceDialog.init().setLayoutId(R.layout.base_dialog_like_ios_alert).setConvertListener(new ViewConvertListener() {
                @Override
                public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {
                    super.convertView(holder, dialog);
                    ImageView ivClose = holder.getView(R.id.iv_close);
                    TextView tvMsg = holder.getView(R.id.tv_msg);
                    TextView tvTitle = holder.getView(R.id.tv_title);
                    RoundButton rbLeft = holder.getView(R.id.rb_left);
                    RoundButton rbRight = holder.getView(R.id.rb_right);
                    if (StringUtils.isNotEmpty(title)) {
                        ViewTools.VISIBLE(tvTitle);
                        ViewTools.setText(tvTitle, title);
                        tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_DIP,16);
                        tvMsg.setTextSize(TypedValue.COMPLEX_UNIT_DIP,14);
                    } else {
                        tvMsg.setTextSize(TypedValue.COMPLEX_UNIT_DIP,16);
                        ViewTools.GONE(tvTitle);
                    }
                    tvMsg.setGravity(gravity);
                    ViewTools.setText(tvMsg, message);
                    ViewTools.setText(rbLeft, leftText);
                    ViewTools.setText(rbRight, rightText);
                    if (showBottomCloseButton) {
                        ViewTools.VISIBLE(ivClose);
                        ivClose.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });
                    } else {
                        ViewTools.GONE(ivClose);
                    }
                    rbLeft.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (leftListener != null) {
                                leftListener.onClick(dialog, Dialog.BUTTON_POSITIVE);
                            }
                        }
                    });
                    rbRight.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (rightListener != null) {
                                rightListener.onClick(dialog, Dialog.BUTTON_NEGATIVE);
                            }
                        }
                    });
                    if (isConfirmDialog) {
                        ViewTools.GONE(rbLeft);
                    }
                }
            }).setMargin(UIUtils.dip2px(30)).setOutCancel(isCancel).show(mManager);
        }
    }
}
